package com.LeeCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 子集
 */

public class Code078 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};

        System.out.println(new Code078().subsets(nums));
    }

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        int n = nums.length;
        findSubsets(nums, ans, new ArrayList<>(), 0, n);
        return ans;
    }

    public void findSubsets(int[] nums, List<List<Integer>> ans, List<Integer> output, int index, int max) {
        if (index == max) {
            ans.add(new ArrayList<>(output));
        }
        if (index < max) {
            // 添加的顺序不同
//            output.add(nums[index]);
//            findSubsets(nums, ans, output, index + 1, max);
//            output.remove((Integer) nums[index]);
//            findSubsets(nums, ans, output, index + 1, max);
            findSubsets(nums, ans, output, index + 1, max);
            output.add(nums[index]);
            findSubsets(nums, ans, output, index + 1, max);
            output.remove(output.size() - 1);
        }
    }
}
